/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.own.service;

import java.util.UUID;

import com.jeesite.common.datasource.DataSourceHolder;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.own.utils.MessageSaveUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.own.entity.OwnOutContents;
import com.jeesite.modules.own.dao.OwnOutContentsDao;

import static org.springframework.util.ObjectUtils.isEmpty;

/**
 * 出库信息目录Service
 * @author F
 * @version 2019-10-11
 */
@Service
@Transactional(readOnly=true)
public class OwnOutContentsService extends CrudService<OwnOutContentsDao, OwnOutContents> {

	@Autowired
	private MessageSaveUtils messageSaveUtils;
	/**
	 * 获取单条数据
	 * @param ownOutContents
	 * @return
	 */
	@Override
	public OwnOutContents get(OwnOutContents ownOutContents) {
		return super.get(ownOutContents);
	}
	
	/**
	 * 查询分页数据
	 * @param ownOutContents 查询条件
	 * @param ownOutContents.page 分页对象
	 * @return
	 */
	@Override
	public Page<OwnOutContents> findPage(OwnOutContents ownOutContents) {
		return super.findPage(ownOutContents);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param ownOutContents
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(OwnOutContents ownOutContents) {
		if(!isEmpty(UserUtils.getUser().getExtend())){
			ownOutContents.setCreateCompany(UserUtils.getUser().getExtend().getExtendS1());
			String uuid = "";
			//如果是新数据则生成uuid
			if(StringUtils.isBlank(ownOutContents.getId())){
				uuid = UUID.randomUUID().toString();
				ownOutContents.setUuid(uuid);
			}else{
				//非新数据，直接获取uuid
				uuid = ownOutContents.getUuid();
			}
			String txid = messageSaveUtils.getTxid(uuid, "出库信息目录表");
			//System.out.println("txid = " + txid);
			ownOutContents.setTxid(txid);
		}
		super.save(ownOutContents);
		DataSourceHolder.setDataSourceName("ds2");
		super.save(ownOutContents);
		DataSourceHolder.clearDataSourceName();
	}
	
	/**
	 * 更新状态
	 * @param ownOutContents
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(OwnOutContents ownOutContents) {
		super.updateStatus(ownOutContents);
	}
	
	/**
	 * 删除数据
	 * @param ownOutContents
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(OwnOutContents ownOutContents) {
		super.delete(ownOutContents);
	}
	
}